#define _CAPTURA_CPP_
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#include <sys/time.h>

#include "callbacks.h"

#include "interface.h"
#include "support.h"
#include "serial.h"
#include "../pic_code/PICdata.h"

#include "support.h"
#include "other_functions.h"

#include "frame_header.hh"

#include "interface_capture.h"
#include "captura.h"
#include "socket.h"

#include "face_detect_vj.hh"
#include "shutter.h"

void get_options(int argc,char *argv[], camera_control *cameras);
extern socket_params_t socket_params;



void * pthread_func_lucasycande(void * params)
{
    captura_params_t *captura = (captura_params_t *)params;
    image_params_t *img_buf;
    IplImage * img_tmp;

    CvRect haar_rect;
    HaarDetector haar;
    haar.load(NULL);
    
    //camera_control * camera = captura->camera;


    while(captura->queue_img.empty())
    {
	if (captura->quit)
	{
	    break;
	}
	usleep(TIME_SLEEP_COMPRESOR_USEC*(rand()%4));
    }


    img_buf = captura->queue_img.back();
    img_tmp = cvCreateImage(cvGetSize(img_buf->img), IPL_DEPTH_8U, 1);


    while (!captura->quit)
    {

	pthread_mutex_lock(&captura->mutex_img);
	//espera a que haya imágenes disponibles para comprimir
	while(captura->queue_img.empty())
	{
	    if (captura->quit)
	    {
		break;
	    }
	    pthread_mutex_unlock(&captura->mutex_img);
	    usleep(TIME_SLEEP_COMPRESOR_USEC*(rand()%4));
	    pthread_mutex_lock(&captura->mutex_img);
	}
	if (captura->quit)
	    break;

	//ya hay imágenes para comprimir
	//extrae la última imagen lista para comprimir
	img_buf = captura->queue_img.back();
	cvCopy(img_buf->img, img_tmp);
	pthread_mutex_unlock(&captura->mutex_img);

	//calcula el viola & jones
	
	haar_rect = haar.detect(img_tmp, HAAR_MIN_SIZE, false);
	Rectangle2(img_tmp, haar_rect, CV_RGB(0,0,0), 2);
	gdk_threads_enter();
	cvShowImage("Viola & Jones", img_tmp);
	gdk_threads_leave();
	
    }
    
    return NULL;

}


void init_gain_control(CvRect *roi, int n, HaarDetector *haar)
{
    bzero(roi, sizeof (CvRect)*n);
    haar->load(NULL);
    
}

double gain_control(IplImage *img, CvRect *roi, camera_control *camera, HaarDetector *haar)
{
    CvScalar mean;
    CvRect roi2 = {0,0, 0, 0};
    //detección de la car
    if (roi->width != 0)
    {
	roi2 = cvRect( roi->x - roi->width/2, 0, roi->width*2, img->height);
	//printf("%d, %d, %d, %d\n", roi2.x, roi2.y, roi2.width, roi2.height);fflush(stdout);
	Rectangle2(img, roi2, CV_RGB(60,60,60), 1);
	cvSetImageROI(img, roi2);
    }
    *roi = haar->detect(img, HAAR_MIN_SIZE, false);
    roi->x += roi2.x;
    roi->y += roi2.y;

    if (roi->width == 0)
    {
	cvResetImageROI(img);
	return 0;
    }

    //control de ganancia de la cámara
    //se aplica sobre el rectángulo detectado del haar
    cvSetImageROI(img, *roi);

    //nivel medio de gris en la roi
    mean = cvAvg(img);
    double var_shutter = (mean.val[0] - MEAN_SHUTTER_PIXEL);
    double max_var_shutter_rel;
    if (var_shutter > MIN_VAR_SHUTTER_PIXEL || var_shutter < - MIN_VAR_SHUTTER_PIXEL)
    {
	max_var_shutter_rel = MAX_VAR_SHUTTER_PIXEL * camera->shutter_value;
	if (var_shutter > max_var_shutter_rel)
	    var_shutter = max_var_shutter_rel;
	else if (var_shutter < -max_var_shutter_rel)
	    var_shutter = -max_var_shutter_rel;

	//pthread_mutex_lock(&mutex_sync);
	camera->shutter_value_new = camera->shutter_value -  var_shutter*MUL_SHUTTER; 

	if (camera->shutter_value_new < MIN_SHUTTER)
	    camera->shutter_value_new = MIN_SHUTTER;
	else if (camera->shutter_value_new > MAX_SHUTTER)
	    camera->shutter_value_new = MAX_SHUTTER;
	//pthread_mutex_unlock(&mutex_sync);

    
	//dc_set_feature(camera, 0, FEATURE_SHUTTER, (int)camera->shutter_value);
    }
    cvResetImageROI(img);
    //Rectangle2(img, *roi, CV_RGB(0,0,0), 2);
    return mean.val[0];

}
